package com.mycompany.myapp.web.rest.controller;


import com.mycompany.myapp.service.CommodityService;
import com.mycompany.myapp.service.dto.CommodityDTO;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api(tags = {"商品管理"})
@RestController
@RequestMapping("/api")
public class CommodityController {
    private CommodityService commodityService;

    public CommodityController(CommodityService commodityService){
        this.commodityService=commodityService;
    }

    @ApiOperation("时尚 品质")
    @GetMapping("/commodity/getyByFashionAndQuality")
    public ResultObj getindexCommodity(){
            return ResultObj.back(200,commodityService.getCommodityByFashionAndQuality());
    }

    @ApiOperation("最新商品")
    @GetMapping("/commodity/getLated")
    public ResultObj getLatedCommodity(){
        return ResultObj.back(200,commodityService.getTheLatestCommodity());
    }

    @ApiOperation("根据类型查询商品")
    @PostMapping("/commodity/select/model")
    public ResultObj selectCommodityByModel(@ApiParam(name="model",value="类型",required=true) @RequestParam String model){
        return ResultObj.back(200,commodityService.findCommodityByType(model));
    }

    @ApiOperation("根据id查询商品")
    @PostMapping("/commodity/select/id")
    public ResultObj selectCommodityById(@ApiParam(name="id",value="商品id",required=true) @RequestParam long id){
        return ResultObj.back(200,commodityService.findCommodityById(id));
    }

    @ApiOperation("新增商品")
    @PostMapping("/commodity/insert")
    public ResultObj insertCommodity(@Valid @RequestBody CommodityDTO commodityDTO){
        return commodityService.insertCommodity(commodityDTO);
    }

    @ApiOperation("更新商品")
    @PostMapping("/commodity/update")
    public ResultObj updateCommodity(@Valid @RequestBody CommodityDTO commodityDTO){
            return commodityService.updateCommodity(commodityDTO);
    }

    @ApiOperation("根据名称查询商品(模糊查询)")
    @PostMapping("/commodity/select/name")
    public ResultObj selectCommodityByName(@ApiParam(name="name",value="商品名",required=true) @RequestParam String name){
        return ResultObj.back(200,commodityService.findCommodityByName(name));
    }

}
